<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Coverage for unittest_modbus_tcp</title>
<link rel='stylesheet' href='style.css' type='text/css'>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
<script type='text/javascript'>
function toggle_lines(btn, cls) {
    var btn = $(btn);
    var hide = "hide_"+cls;
    if (btn.hasClass(hide)) {
        $("#source ."+cls).removeClass(hide);
        btn.removeClass(hide);
    }
    else {
        $("#source ."+cls).addClass(hide);
        btn.addClass(hide);
    }
}
</script>
</head>
<body>
<div id='header'>
    <div class='content'>
        <h1>Coverage for <b>unittest_modbus_tcp</b> :
            <span class='pc_cov'>97%</span>
        </h1>
        <h2 class='stats'>
            201 statements
            <span class='run hide_run' onclick='toggle_lines(this, "run")'>196 run</span>
            <span class='exc' onclick='toggle_lines(this, "exc")'>0 excluded</span>
            <span class='mis' onclick='toggle_lines(this, "mis")'>5 missing</span>
            
                <span class='par run hide_run' onclick='toggle_lines(this, "par")'>1 partial</span>
            
        </h2>
    </div>
</div>

<div id='source'>
<table cellspacing='0' cellpadding='0'>
<tr>
<td class='linenos' valign='top'>
<p class='pln'>1</p>
<p class='pln'>2</p>
<p class='stm run hide_run'>3</p>
<p class='pln'>4</p>
<p class='pln'>5</p>
<p class='pln'>6</p>
<p class='pln'>7</p>
<p class='pln'>8</p>
<p class='pln'>9</p>
<p class='pln'>10</p>
<p class='pln'>11</p>
<p class='stm run hide_run'>12</p>
<p class='stm run hide_run'>13</p>
<p class='stm run hide_run'>14</p>
<p class='stm run hide_run'>15</p>
<p class='stm run hide_run'>16</p>
<p class='stm run hide_run'>17</p>
<p class='pln'>18</p>
<p class='stm run hide_run'>19</p>
<p class='pln'>20</p>
<p class='stm run hide_run'>21</p>
<p class='pln'>22</p>
<p class='stm run hide_run'>23</p>
<p class='stm run hide_run'>24</p>
<p class='pln'>25</p>
<p class='stm run hide_run'>26</p>
<p class='stm run hide_run'>27</p>
<p class='stm run hide_run'>28</p>
<p class='stm run hide_run'>29</p>
<p class='pln'>30</p>
<p class='pln'>31</p>
<p class='stm run hide_run'>32</p>
<p class='stm run hide_run'>33</p>
<p class='pln'>34</p>
<p class='stm run hide_run'>35</p>
<p class='pln'>36</p>
<p class='stm run hide_run'>37</p>
<p class='pln'>38</p>
<p class='stm run hide_run'>39</p>
<p class='pln'>40</p>
<p class='stm run hide_run'>41</p>
<p class='stm run hide_run'>42</p>
<p class='stm run hide_run'>43</p>
<p class='stm run hide_run'>44</p>
<p class='pln'>45</p>
<p class='stm run hide_run'>46</p>
<p class='pln'>47</p>
<p class='stm run hide_run'>48</p>
<p class='pln'>49</p>
<p class='stm run hide_run'>50</p>
<p class='stm run hide_run'>51</p>
<p class='stm run hide_run'>52</p>
<p class='stm run hide_run'>53</p>
<p class='stm run hide_run'>54</p>
<p class='pln'>55</p>
<p class='stm run hide_run'>56</p>
<p class='pln'>57</p>
<p class='stm run hide_run'>58</p>
<p class='pln'>59</p>
<p class='stm run hide_run'>60</p>
<p class='stm run hide_run'>61</p>
<p class='stm run hide_run'>62</p>
<p class='stm run hide_run'>63</p>
<p class='stm run hide_run'>64</p>
<p class='pln'>65</p>
<p class='stm run hide_run'>66</p>
<p class='pln'>67</p>
<p class='stm run hide_run'>68</p>
<p class='pln'>69</p>
<p class='stm run hide_run'>70</p>
<p class='stm run hide_run'>71</p>
<p class='stm run hide_run'>72</p>
<p class='stm run hide_run'>73</p>
<p class='stm run hide_run'>74</p>
<p class='pln'>75</p>
<p class='stm run hide_run'>76</p>
<p class='pln'>77</p>
<p class='stm run hide_run'>78</p>
<p class='pln'>79</p>
<p class='stm run hide_run'>80</p>
<p class='stm run hide_run'>81</p>
<p class='stm run hide_run'>82</p>
<p class='stm run hide_run'>83</p>
<p class='stm run hide_run'>84</p>
<p class='pln'>85</p>
<p class='stm run hide_run'>86</p>
<p class='pln'>87</p>
<p class='stm run hide_run'>88</p>
<p class='pln'>89</p>
<p class='stm run hide_run'>90</p>
<p class='stm run hide_run'>91</p>
<p class='stm run hide_run'>92</p>
<p class='stm run hide_run'>93</p>
<p class='stm run hide_run'>94</p>
<p class='pln'>95</p>
<p class='stm run hide_run'>96</p>
<p class='pln'>97</p>
<p class='stm run hide_run'>98</p>
<p class='pln'>99</p>
<p class='stm run hide_run'>100</p>
<p class='pln'>101</p>
<p class='stm run hide_run'>102</p>
<p class='pln'>103</p>
<p class='stm run hide_run'>104</p>
<p class='stm run hide_run'>105</p>
<p class='pln'>106</p>
<p class='stm run hide_run'>107</p>
<p class='stm run hide_run'>108</p>
<p class='stm run hide_run'>109</p>
<p class='stm run hide_run'>110</p>
<p class='pln'>111</p>
<p class='stm run hide_run'>112</p>
<p class='pln'>113</p>
<p class='stm run hide_run'>114</p>
<p class='stm run hide_run'>115</p>
<p class='stm run hide_run'>116</p>
<p class='pln'>117</p>
<p class='stm run hide_run'>118</p>
<p class='stm run hide_run'>119</p>
<p class='pln'>120</p>
<p class='stm run hide_run'>121</p>
<p class='pln'>122</p>
<p class='stm run hide_run'>123</p>
<p class='stm run hide_run'>124</p>
<p class='stm run hide_run'>125</p>
<p class='stm run hide_run'>126</p>
<p class='pln'>127</p>
<p class='stm run hide_run'>128</p>
<p class='stm run hide_run'>129</p>
<p class='stm run hide_run'>130</p>
<p class='stm run hide_run'>131</p>
<p class='stm run hide_run'>132</p>
<p class='stm run hide_run'>133</p>
<p class='pln'>134</p>
<p class='stm run hide_run'>135</p>
<p class='pln'>136</p>
<p class='stm run hide_run'>137</p>
<p class='stm run hide_run'>138</p>
<p class='stm run hide_run'>139</p>
<p class='stm run hide_run'>140</p>
<p class='stm run hide_run'>141</p>
<p class='pln'>142</p>
<p class='stm run hide_run'>143</p>
<p class='pln'>144</p>
<p class='stm run hide_run'>145</p>
<p class='pln'>146</p>
<p class='stm run hide_run'>147</p>
<p class='stm run hide_run'>148</p>
<p class='pln'>149</p>
<p class='stm run hide_run'>150</p>
<p class='stm run hide_run'>151</p>
<p class='pln'>152</p>
<p class='stm run hide_run'>153</p>
<p class='pln'>154</p>
<p class='stm run hide_run'>155</p>
<p class='stm run hide_run'>156</p>
<p class='stm run hide_run'>157</p>
<p class='pln'>158</p>
<p class='stm run hide_run'>159</p>
<p class='pln'>160</p>
<p class='stm run hide_run'>161</p>
<p class='stm run hide_run'>162</p>
<p class='stm run hide_run'>163</p>
<p class='stm run hide_run'>164</p>
<p class='pln'>165</p>
<p class='stm run hide_run'>166</p>
<p class='pln'>167</p>
<p class='stm run hide_run'>168</p>
<p class='stm run hide_run'>169</p>
<p class='stm run hide_run'>170</p>
<p class='pln'>171</p>
<p class='stm run hide_run'>172</p>
<p class='pln'>173</p>
<p class='stm run hide_run'>174</p>
<p class='stm run hide_run'>175</p>
<p class='stm run hide_run'>176</p>
<p class='stm run hide_run'>177</p>
<p class='pln'>178</p>
<p class='stm run hide_run'>179</p>
<p class='pln'>180</p>
<p class='stm run hide_run'>181</p>
<p class='stm run hide_run'>182</p>
<p class='stm run hide_run'>183</p>
<p class='stm run hide_run'>184</p>
<p class='stm run hide_run'>185</p>
<p class='stm run hide_run'>186</p>
<p class='pln'>187</p>
<p class='stm run hide_run'>188</p>
<p class='pln'>189</p>
<p class='stm run hide_run'>190</p>
<p class='stm run hide_run'>191</p>
<p class='stm run hide_run'>192</p>
<p class='pln'>193</p>
<p class='stm run hide_run'>194</p>
<p class='pln'>195</p>
<p class='stm run hide_run'>196</p>
<p class='stm run hide_run'>197</p>
<p class='stm run hide_run'>198</p>
<p class='stm run hide_run'>199</p>
<p class='stm run hide_run'>200</p>
<p class='pln'>201</p>
<p class='stm run hide_run'>202</p>
<p class='pln'>203</p>
<p class='stm run hide_run'>204</p>
<p class='stm run hide_run'>205</p>
<p class='stm run hide_run'>206</p>
<p class='stm run hide_run'>207</p>
<p class='stm run hide_run'>208</p>
<p class='pln'>209</p>
<p class='stm run hide_run'>210</p>
<p class='pln'>211</p>
<p class='stm run hide_run'>212</p>
<p class='stm run hide_run'>213</p>
<p class='stm run hide_run'>214</p>
<p class='stm run hide_run'>215</p>
<p class='stm run hide_run'>216</p>
<p class='pln'>217</p>
<p class='stm run hide_run'>218</p>
<p class='pln'>219</p>
<p class='stm mis'>220</p>
<p class='stm mis'>221</p>
<p class='stm mis'>222</p>
<p class='stm mis'>223</p>
<p class='stm mis'>224</p>
<p class='pln'>225</p>
<p class='stm run hide_run'>226</p>
<p class='pln'>227</p>
<p class='stm run hide_run'>228</p>
<p class='stm run hide_run'>229</p>
<p class='stm run hide_run'>230</p>
<p class='stm run hide_run'>231</p>
<p class='stm run hide_run'>232</p>
<p class='pln'>233</p>
<p class='stm run hide_run'>234</p>
<p class='pln'>235</p>
<p class='stm run hide_run'>236</p>
<p class='stm run hide_run'>237</p>
<p class='stm run hide_run'>238</p>
<p class='pln'>239</p>
<p class='stm run hide_run'>240</p>
<p class='pln'>241</p>
<p class='stm run hide_run'>242</p>
<p class='stm run hide_run'>243</p>
<p class='stm run hide_run'>244</p>
<p class='stm run hide_run'>245</p>
<p class='stm run hide_run'>246</p>
<p class='stm run hide_run'>247</p>
<p class='stm run hide_run'>248</p>
<p class='stm run hide_run'>249</p>
<p class='pln'>250</p>
<p class='stm run hide_run'>251</p>
<p class='pln'>252</p>
<p class='stm run hide_run'>253</p>
<p class='stm run hide_run'>254</p>
<p class='stm run hide_run'>255</p>
<p class='stm run hide_run'>256</p>
<p class='stm run hide_run'>257</p>
<p class='pln'>258</p>
<p class='stm run hide_run'>259</p>
<p class='pln'>260</p>
<p class='stm run hide_run'>261</p>
<p class='stm run hide_run'>262</p>
<p class='stm run hide_run'>263</p>
<p class='stm run hide_run'>264</p>
<p class='stm run hide_run'>265</p>
<p class='stm run hide_run'>266</p>
<p class='stm run hide_run'>267</p>
<p class='stm run hide_run'>268</p>
<p class='pln'>269</p>
<p class='stm run hide_run'>270</p>
<p class='stm run hide_run'>271</p>
<p class='stm run hide_run'>272</p>
<p class='pln'>273</p>
<p class='stm run hide_run'>274</p>
<p class='pln'>275</p>
<p class='stm run hide_run'>276</p>
<p class='stm run hide_run'>277</p>
<p class='stm run hide_run'>278</p>
<p class='pln'>279</p>
<p class='stm run hide_run'>280</p>
<p class='stm run hide_run'>281</p>
<p class='pln'>282</p>
<p class='stm run hide_run'>283</p>
<p class='pln'>284</p>
<p class='stm run hide_run'>285</p>
<p class='stm run hide_run'>286</p>
<p class='stm run hide_run'>287</p>
<p class='stm run hide_run'>288</p>
<p class='pln'>289</p>
<p class='stm par run hide_run'>290</p>
<p class='stm run hide_run'>291</p>
    
</td>
<td class='text' valign='top'>
<p class='pln'><span class='com'>#!/usr/bin/env python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'># -*- coding: utf-8 -*-</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> Modbus TestKit: Implementation of Modbus protocol in python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Luc Jean - luc.jean@gmail.com</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Apidev - http://www.apidev.fr</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> This is distributed under GNU LGPL license, see license.txt</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>unittest</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>modbus_tk</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span> <span class='key'>as</span> <span class='nam'>modbus_tcp</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>threading</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>struct</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>logging</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='nam'>LOGGER</span> <span class='op'>=</span> <span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>create_logger</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>TestMbap</span><span class='op'>(</span><span class='nam'>unittest</span><span class='op'>.</span><span class='nam'>TestCase</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test the TcpMbap class&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>setUp</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>3</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>4</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>tearDown</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testClone</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;test the clone function makes a copy of the object&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>clone</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>length</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertNotEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckIds</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the check ids pass with correct mbap&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>10</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>4</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>check_response</span><span class='op'>(</span><span class='nam'>mbap2</span><span class='op'>,</span> <span class='num'>3</span><span class='op'>-</span><span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckIdsWrongLength</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the check ids fails when the length is not Ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>10</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>4</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>check_response</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckIdsWrongTransactionId</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the check ids fails when the transaction id is not Ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>10</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>4</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>check_response</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>,</span> <span class='num'>2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckIdsWrongProtocolId</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the check ids fails when the transaction id is not Ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>3</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>10</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>4</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>check_response</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>,</span> <span class='num'>2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckIdsWrongUnitId</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the check ids fails when the transaction id is not Ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span> <span class='op'>=</span> <span class='num'>2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span> <span class='op'>=</span> <span class='num'>10</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span> <span class='op'>=</span> <span class='num'>5</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>check_response</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>,</span> <span class='num'>2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testPack</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that packing a mbap give the expected result&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>,</span> <span class='num'>2</span><span class='op'>,</span> <span class='num'>3</span><span class='op'>,</span> <span class='num'>4</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testUnpack</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that unpacking a mbap give the expected result&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpMbap</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>length</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>length</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertNotEqual</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>mbap1</span><span class='op'>,</span> <span class='nam'>mbap2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>TestTcpQuery</span><span class='op'>(</span><span class='nam'>unittest</span><span class='op'>.</span><span class='nam'>TestCase</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>setUp</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>tearDown</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testIncTrIdIsThreadSafe</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Check that the function in charge of increasing the transaction id is thread safe&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>inc_by</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>1000</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>tr_id_before</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>threads</span> <span class='op'>=</span> <span class='op'>[</span><span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Thread</span><span class='op'>(</span><span class='nam'>target</span><span class='op'>=</span><span class='nam'>inc_by</span><span class='op'>)</span> <span class='key'>for</span> <span class='nam'>thread_nr</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>20</span><span class='op'>)</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>thread</span> <span class='key'>in</span> <span class='nam'>threads</span><span class='op'>:</span> <span class='nam'>thread</span><span class='op'>.</span><span class='nam'>start</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>thread</span> <span class='key'>in</span> <span class='nam'>threads</span><span class='op'>:</span> <span class='nam'>thread</span><span class='op'>.</span><span class='nam'>join</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='num'>1000</span><span class='op'>*</span><span class='num'>20</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='op'>-</span><span class='nam'>tr_id_before</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testCheckTrIdRollover</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Check that the transaction id will rollover when max valuie is reached&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>tr_id_before</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>a</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>int</span><span class='op'>(</span><span class='str'>&quot;ffff&quot;</span><span class='op'>,</span> <span class='num'>16</span><span class='op'>)</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>query</span><span class='op'>.</span><span class='nam'>_get_transaction_id</span><span class='op'>(</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>tr_id_before</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testIncIdOfRequest</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Check that the transaction id is increased when building the request&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>queries</span> <span class='op'>=</span> <span class='op'>[</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span> <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>100</span><span class='op'>)</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>queries</span><span class='op'>)</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>queries</span><span class='op'>[</span><span class='nam'>i</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>queries</span><span class='op'>)</span><span class='op'>-</span><span class='num'>1</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>queries</span><span class='op'>[</span><span class='nam'>i</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>queries</span><span class='op'>[</span><span class='nam'>i</span><span class='op'>+</span><span class='num'>1</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testBuildRequest</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test the mbap returned by building a request&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testBuildRequestWithSlave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test the mbap returned by building a request with a slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>0</span><span class='op'>,</span> <span class='num'>255</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testBuildRequestWithInvalidSlave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that an error is raised when invalid slave is passed&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='op'>[</span><span class='op'>-</span><span class='num'>1</span><span class='op'>,</span> <span class='num'>256</span><span class='op'>,</span> <span class='num'>257</span><span class='op'>,</span> <span class='num'>65536</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>InvalidArgumentError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>,</span> <span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testBuildRequestWithPdu</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test the mbap returned by building a request with a pdu&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>pdu</span> <span class='key'>in</span> <span class='op'>[</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>127</span><span class='op'>,</span> <span class='str'>&quot;abcdefghi&quot;</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseRespone</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that Modbus TCP part of the response is understood&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>pdu</span> <span class='key'>in</span> <span class='op'>[</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>127</span><span class='op'>,</span> <span class='str'>&quot;abcdefghi&quot;</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>extracted</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>extracted</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseTooShortRespone</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if the response is too short&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='str'>&quot;&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>6</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseWrongSlaveResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if the slave id is wrong&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;a&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseWrongTransactionResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if wrong transaction id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;a&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseWrongProtocolIdResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if wrong protocol id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;a&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseWrongLengthResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if the length is not ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;a&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseWrongLengthResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if the length is not ok&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;a&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>+</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;s&quot;</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>transaction_id</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>protocol_id</span><span class='op'>+</span><span class='num'>1</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>_request_mbap</span><span class='op'>.</span><span class='nam'>unit_id</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseTooShortRequest</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test an error is raised if the request is too short&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_request</span><span class='op'>,</span> <span class='str'>&quot;&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_request</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>6</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseRequest</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that Modbus TCP part of the request is understood&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>pdu</span> <span class='key'>in</span> <span class='op'>[</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>127</span><span class='op'>,</span> <span class='str'>&quot;abcdefghi&quot;</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>slave</span><span class='op'>,</span> <span class='nam'>extracted_pdu</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_request</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>extracted_pdu</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>slave</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testParseRequestInvalidLength</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that an error is raised if the length is not valid&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>pdu</span> <span class='key'>in</span> <span class='op'>[</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>127</span><span class='op'>,</span> <span class='str'>&quot;abcdefghi&quot;</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='op'>+</span><span class='num'>2</span><span class='op'>)</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>ModbusInvalidMbapError</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_request</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>+</span><span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testBuildResponse</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test that the response of a request is build properly&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>pdu</span> <span class='key'>in</span> <span class='op'>[</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>,</span> <span class='str'>&quot;a&quot;</span><span class='op'>*</span><span class='num'>127</span><span class='op'>,</span> <span class='str'>&quot;abcdefghi&quot;</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_response</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response_pdu</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>TestTcpServer</span><span class='op'>(</span><span class='nam'>unittest</span><span class='op'>.</span><span class='nam'>TestCase</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>setUp</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span> <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>tearDown</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span> <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testGetRequestLength</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test than _get_request_length returns the length field of request mbap&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>s</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpServer</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHHB&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>12</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>s</span><span class='op'>.</span><span class='nam'>_get_request_length</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='op'>,</span> <span class='num'>12</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHH&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>129</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertEqual</span><span class='op'>(</span><span class='nam'>s</span><span class='op'>.</span><span class='nam'>_get_request_length</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='op'>,</span> <span class='num'>129</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>testGetRequestLengthFailsOnInvalid</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Test than an error is raised in _get_request_length is the length field of request mbap is not filled&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>s</span> <span class='op'>=</span> <span class='nam'>modbus_tcp</span><span class='op'>.</span><span class='nam'>TcpServer</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHB&quot;</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='nam'>s</span><span class='op'>.</span><span class='nam'>_get_request_length</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>assertRaises</span><span class='op'>(</span><span class='nam'>modbus_tk</span><span class='op'>.</span><span class='nam'>modbus</span><span class='op'>.</span><span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='nam'>s</span><span class='op'>.</span><span class='nam'>_get_request_length</span><span class='op'>,</span> <span class='str'>&quot;&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>exit</span><span class='key'>if</span> <span class='nam'>__name__</span> <span class='op'>==</span> <span class='str'>&#39;__main__&#39;</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>unittest</span><span class='op'>.</span><span class='nam'>main</span><span class='op'>(</span><span class='nam'>argv</span> <span class='op'>=</span> <span class='nam'>unittest</span><span class='op'>.</span><span class='nam'>sys</span><span class='op'>.</span><span class='nam'>argv</span> <span class='op'>+</span> <span class='op'>[</span><span class='str'>&#39;--verbose&#39;</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
    
</td>
</tr>
</table>
</div>

</body>
</html>
